/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.coinche.client.ui.card.impl;

import org.jb.coinche.client.business.handler.DisconnectHandler;
import org.jb.coinche.client.ui.card.AuctionPanel;
import org.jb.coinche.client.ui.card.CardList;
import org.jb.coinche.client.ui.card.ScorePanel;
import org.jb.coinche.client.ui.card.SetPanel;
import org.jb.coinche.client.ui.card.StatusPanel;
import org.jb.ui.chat.client.ChatWidget;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Layer.ShadowPosition;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class GameWindow extends LayoutContainer {

	@Inject
	public GameWindow( ChatWidget chatWidget, final DisconnectHandler disconnectHandler ) {		
		setSize( 750, 550 );
		setLayout( new BorderLayout() );
		setShadow(true);
		setShadowOffset(20);
		setShadowPosition(ShadowPosition.DROP);
		
		Button btnDisconnect = new Button("Quitter la table");
		btnDisconnect.addSelectionListener( new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				disconnectHandler.disconnect();
			}
		});
		
		RootPanel.get("disconnect").add(btnDisconnect);
		
		ContentPanel north = new ContentPanel( new RowLayout(Orientation.HORIZONTAL));
		north.setBodyBorder(false);
		north.setHeaderVisible(false);
		
		setPanel = new CrossSetPanel();
		north.add( setPanel );
	    
		ContentPanel cpData = new ContentPanel( new RowLayout(Orientation.VERTICAL));
		cpData.setWidth("100%");
		cpData.setAutoHeight(true);
		cpData.setHeaderVisible(false);
		cpData.setBodyBorder(true);
		
		ContentPanel cpCardList = new ContentPanel( new FitLayout() );
		cpCardList.setBodyBorder(false);
		cpCardList.setHeaderVisible(false);
	    cardList = new CardListImpl();
	    cpCardList.add( cardList );
	    
	    auctionTb = new AuctionToolbar();
	    auctionTb.refresh();
	    cpCardList.setTopComponent(auctionTb);
	    
	    cpData.add(cpCardList);
	    
	    ContentPanel lastPlay = new ContentPanel( new FitLayout() );
	    lastPlay.setHeading("Dernier pli");
	    lastPlay.setSize( 370, 90 );
	    playedSetPanel = new LineSetPanel();
	    lastPlay.add( playedSetPanel );
	    cpData.add(lastPlay, new RowData(-1, -1, new Margins(5) ) );
	    
	    final ContentPanel score = new ContentPanel( new FitLayout() );
	    score.setHeading("Status");
	    score.setSize(370, 80);
	    statusPanel = new StatusPanelImpl();
	    score.add( statusPanel );
	    
	    globalScorePanel = new ScorePanel() {
			
			@Override
			public void setScore(int scoreTeam1, int scoreTeam2) {
				score.setHeading("Status:  T1=" + Integer.toString(scoreTeam1) + "points - T2=" + Integer.toString(scoreTeam2) + "points");
			}
		};
	    
	    cpData.add(score, new RowData(-1, -1, new Margins(5) ) );
	    
	    north.add( cpData );
	    
		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.NORTH, 370);  
	    northData.setMargins(new Margins(0));  
	    
	    add( north, northData );
		
		BorderLayoutData southData = new BorderLayoutData(LayoutRegion.CENTER, 150);  
	    southData.setMargins(new Margins(5, 0, 0, 0));  
	    
	    ContentPanel south = new ContentPanel( new FitLayout());
		this.chatWidget = chatWidget;
		south.setHeaderVisible(false);
		south.setBodyBorder(false);
		south.add( chatWidget );
		add( south, southData );
	    
	    DOM.getElementById("content").removeAttribute("style");
	    RootPanel.get("gamewindow").add(this);	    
	}

	/**
	 * @return the setPanel
	 */
	public CrossSetPanel getSetPanel() {
		return setPanel;
	}
	
	/**
	 * 
	 * @return
	 */
	public CardList getCardList() {
		return cardList;
	}
	
	/**
	 * 
	 * @return
	 */
	public ChatWidget getChatWidget() {
		return chatWidget;
	}
	
	/*
	 * 
	 */
	public AuctionPanel getAuctionPanel() {
		return auctionTb;
	}

	/*
	 * 
	 */
	public ScorePanel getGlobalScorePanel() {
		return globalScorePanel;
	}
	
	public ScorePanel getRoundScorePanel() {
		return NullScorePanel.INSTANCE;
	}
	
	public StatusPanel getStatusPanel() {
		return statusPanel;
	}
	
	public SetPanel getPlayedSetPanel() {
		return playedSetPanel;
	}

	public static class NullScorePanel implements ScorePanel {

		public final static NullScorePanel INSTANCE = new NullScorePanel();
		
		@Override
		public void setScore(int scoreTeam1, int scoreTeam2) {}
		
	}
	
	final private CrossSetPanel setPanel;
	final private CardListImpl cardList;
	final private ChatWidget chatWidget;
	final private StatusPanelImpl statusPanel;
	final private ScorePanel globalScorePanel;
	private AuctionToolbar auctionTb;
	private LineSetPanel playedSetPanel;
}
